using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class MapCreation : MonoBehaviour
{
    public GameObject[] item;
    private List<Vector3> itemPositionList = new List<Vector3>();

    // 敌人生成的坐标
    private Vector3[] enemyPositions = new Vector3[]
    {
        new Vector3(-10, 8, 0),
        new Vector3(0, 8, 0),
        new Vector3(10, 8, 0)
    };

    // 敌人总数
    private int totalEnemies =8; // 修改为8

    // 剩余的敌人数量
    public static int enemySurplus =0;

    // 每个坐标点初始生成的敌人数量
    private int[] initialSpawnsPerPoint = new int[] { 1, 1, 1 };

    private void Start()
    {
        //生成敌人个数
        // totalEnemies = 8;
        GenerateInitialEnemies();
        StartCoroutine(GenerateRemainingEnemies());
    }

    /**
     * 生成初始的敌人
     */
    private void GenerateInitialEnemies()
    {
        Debug.Log("敌人总数: " + totalEnemies);
        Debug.Log("每个坐标点初始生成的敌人数量.Sum(): " + initialSpawnsPerPoint.Sum());
        // 计算剩余需要生成的敌人数量
        int remainingEnemies = totalEnemies - initialSpawnsPerPoint.Sum();
        Debug.Log("待刷出的剩余敌人: " + remainingEnemies);
        // 游戏开始时生成敌人
        for (int i = 0; i < enemyPositions.Length; i++)
        {
            Vector3 position = enemyPositions[i];
            int count = initialSpawnsPerPoint[i];

            // 立即生成一定数量的敌人
            for (int j = 0; j < count; j++)
            {
                CreateItem(item[3], position, Quaternion.identity);

                enemySurplus++;
            }
        }
    }


    /**
     * 生成剩余的敌人
     */
    private IEnumerator GenerateRemainingEnemies()
    {

        Debug.Log("1敌人总数: " + totalEnemies);
        Debug.Log("1每个坐标点初始生成的敌人数量.Sum(): " + initialSpawnsPerPoint.Sum());
        // 计算剩余需要生成的敌人数量
        int remainingEnemies = totalEnemies - initialSpawnsPerPoint.Sum();
        Debug.Log("1待刷出的剩余敌人: " + remainingEnemies);

        int enemiesToSpawnThisRound = Mathf.Min(remainingEnemies, enemyPositions.Length);

        while (remainingEnemies > 0)
        {
            //语句用于暂停协程的执行，使协程在指定的时间（这里是5秒）后继续执行。这是 Unity 中异步编程的一部分，用于实现延时执行的效果。
            yield return new WaitForSeconds(5);

            for (int i = 0; i < enemiesToSpawnThisRound; i++)
            {
                if (remainingEnemies > 0)
                {
                    Vector3 position = enemyPositions[i % enemyPositions.Length];
                    CreateItem(item[3], position, Quaternion.identity);
                    remainingEnemies--;
                    Debug.Log("生成的敌人: " + position + " 剩余的敌人: " + remainingEnemies);
                     enemySurplus++;
                }
                else
                {
                    Debug.Log("敌人生成结束，不再继续生成");
                    break;
                }
            }

            enemiesToSpawnThisRound = Mathf.Min(remainingEnemies, enemyPositions.Length);
            Debug.Log("敌人将在此回合出现: " + enemiesToSpawnThisRound);
        }
    }

    private void Awake()
    {
        // 实例化老家                              无旋转
        CreateItem(item[0], new Vector3(0, -8, 0), Quaternion.identity);

        // 用墙把老家围起来
        CreateItem(item[2], new Vector3(-1, -8, 0), Quaternion.identity);
        CreateItem(item[2], new Vector3(1, -8, 0), Quaternion.identity);
        for (int i = -1; i < 2; i++)
        {
            CreateItem(item[2], new Vector3(i, -7, 0), Quaternion.identity);
        }

        // 实例外围墙
        // for (int i = -11; i < 12; i++)
        // {
        //     CreateItem(item[6], new Vector3(i, 9, 0), Quaternion.identity);
        //     CreateItem(item[6], new Vector3(i, -9, 0), Quaternion.identity);
        // }

        // for (int i = -8; i < 9; i++)
        // {
        //     CreateItem(item[6], new Vector3(-11, i, 0), Quaternion.identity);
        //     CreateItem(item[6], new Vector3(11, i, 0), Quaternion.identity);
        // }

        // 初始化玩家
        GameObject go = Instantiate(item[3], new Vector3(-2, -8, 0), Quaternion.identity);
        go.GetComponent<Born>().createPlayer = true;

        // 实例化地图  //0.老家 1.墙 2.障碍 3.出生效果 4.河流 5.草 6.空气墙
        for (int i = 0; i < 80; i++)
        {
            // 墙
            CreateItem(item[1], CreateRandomPosition(), Quaternion.identity);
        }
        for (int i = 0; i < 20; i++)
        {
            // 障碍
            CreateItem(item[2], CreateRandomPosition(), Quaternion.identity);
        }
        for (int i = 0; i < 10; i++)
        {
            // 河流
            CreateItem(item[4], CreateRandomPosition(), Quaternion.identity);
        }
        for (int i = 0; i < 20; i++)
        {
            // 草
            CreateItem(item[5], CreateRandomPosition(), Quaternion.identity);
        }
    }
    /**
     * 创建物品
     */
    private void CreateItem(GameObject createCameObject, Vector3 createPosition, Quaternion createRotation)
    {
        GameObject itemGo = Instantiate(createCameObject, createPosition, createRotation);
        itemGo.transform.SetParent(gameObject.transform);
        itemPositionList.Add(createPosition);
    }

    /**
     * 随机生成坐标
     */
    private Vector3 CreateRandomPosition()
    {
        while (true)
        {
            Vector3 cratePosition = new Vector3(Random.Range(-9, 10), Random.Range(-7, 8), 0);
            if (!HasThePosition(cratePosition))
            {
                return cratePosition;
            }
        }
    }


    /**
     * 判断当前坐标是否已经存在
     */
    private bool HasThePosition(Vector3 createPos)
    {
        for (int i = 0; i < itemPositionList.Count; i++)
        {
            if (createPos == itemPositionList[i])
            {
                return true;
            }
        }
        return false;
    }
}